/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import javax.swing.table.AbstractTableModel;

import config.ConfigInterface;

@SuppressWarnings("serial")
public class SyncTableModel extends AbstractTableModel {

	private ConfigInterface config;

	private String[] columnNames;
	private String[] columnConfigNames;


	public SyncTableModel(ConfigInterface config, String[] columnNames, String[] columnConfigNames) {
		this.config = config;
		this.columnNames = columnNames;
		this.columnConfigNames = columnConfigNames;
	}


	public int getRowCount() {
		return config.getListSize("client");
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public String getColumnName(int column) {
		return columnNames[column];
	}

	public Object getValueAt(int row, int column) {
		if(column == 0) {
			return row;
		}
		else {
			Object value = config.getListVariable("client", row, columnConfigNames[column]);
			if(value instanceof Integer) {
				return (Integer)value;
			}
			else if(value instanceof Boolean) {
				return (Boolean)value;
			}
			else { // if(value instanceof String)
				return (String)value;
			}
		}
	}

	public Class<?> getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public boolean isCellEditable(int row, int column) {
		// The data/cell address is constant, no matter where the cell appears onscreen.
		if (column == 0) {
			return false;
		} else {
			return true;
		}
	}

	public void setValueAt(Object value, int row, int column) {
		boolean DEBUG = true;
		if (DEBUG) {
			System.out.println("Setting value at " + row + "," + column
					+ " to " + value + " (an instance of " + value.getClass() + ")");
		}

		config.setListVariable("client", row, columnConfigNames[column], value);
		fireTableCellUpdated(row, column);

		if (DEBUG) {
			System.out.println("New value of data:");
			printDebugData();
		}
	}

	private void printDebugData() {
		int numRows = getRowCount();
		int numCols = getColumnCount();

		for (int i=0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j=0; j < numCols; j++) {
				if(j == 0) {
					System.out.print("  " + i);
				}
				else {
					System.out.print("  " + config.getListVariable("client", i, columnConfigNames[j]));
				}
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}

}
